<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\model\AdminPermission;
use think\facade\Db;
use think\facade\Log;

class AdminPermissionCommand extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('my:AdminPermission')
            ->setDescription('写入 admin_permission 表数据');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('start');
        $output->writeln("正在写入 admin_permission 表数据");

        // 除了 $root_permissions[index]['pid']，$permissions[index]['pid'] 都是上级菜单的 title
        $uri    = '#';
        $pid    = 0;
        $path   = '0';
        $fields = ['title', 'uri', 'pid', 'path'];

        $root_permissions = [
            ['菜单', $uri, $pid, $path],
            ['系统参数', $uri, $pid, $path],
            ['管理员', $uri, $pid, $path],
            ['角色', $uri, $pid, $path],
            ['操作日志', $uri, $pid, $path],
            ['登录日志', $uri, $pid, $path],
            ['访问权限', $uri, $pid, $path],
            ['角色权限', $uri, $pid, $path],
            ['角色绑定', $uri, $pid, $path],
        ];
        $permissions = [
            ['菜单-列表', 'admin/menu/index', '菜单', $path],
            ['菜单-创建', 'admin/menu/create', '菜单', $path],
            ['菜单-保存', 'admin/menu/store', '菜单', $path],
            ['菜单-详情', 'admin/menu/show', '菜单', $path],
            ['菜单-编辑', 'admin/menu/edit', '菜单', $path],
            ['菜单-更新', 'admin/menu/update', '菜单', $path],
            ['菜单-删除', 'admin/menu/destroy', '菜单', $path],

            ['系统参数-列表', 'admin/config/index', '系统参数', $path],
            ['系统参数-编辑', 'admin/config/edit', '系统参数', $path],
            ['系统参数-更新', 'admin/config/update', '系统参数', $path],

            ['管理员-列表', 'admin/user/index', '管理员', $path],
            ['管理员-详情', 'admin/user/show', '管理员', $path],
            ['管理员-创建', 'admin/user/create', '管理员', $path],
            ['管理员-编辑', 'admin/user/edit', '管理员', $path],
            ['管理员-保存', 'admin/user/store', '管理员', $path],
            ['管理员-更新', 'admin/user/update', '管理员', $path],
            ['管理员-删除', 'admin/user/destroy', '管理员', $path],

            ['角色-列表', 'admin/role/index', '角色', $path],
            ['角色-详情', 'admin/role/show', '角色', $path],
            ['角色-创建', 'admin/role/create', '角色', $path],
            ['角色-编辑', 'admin/role/edit', '角色', $path],
            ['角色-保存', 'admin/role/store', '角色', $path],
            ['角色-更新', 'admin/role/update', '角色', $path],
            ['角色-删除', 'admin/role/destroy', '角色', $path],

            ['操作日志-列表', 'admin/action-log/index', '操作日志', $path],
            ['操作日志-详情', 'admin/action-log/show', '操作日志', $path],

            ['登录日志-列表', 'admin/user-login-log/index', '登录日志', $path],
            ['登录日志-详情', 'admin/user-login-log/show', '登录日志', $path],

            ['访问权限-列表', 'admin/permission/index', '访问权限', $path],
            ['访问权限-详情', 'admin/permission/show', '访问权限', $path],
            ['访问权限-创建', 'admin/permission/create', '访问权限', $path],
            ['访问权限-编辑', 'admin/permission/edit', '访问权限', $path],
            ['访问权限-保存', 'admin/permission/store', '访问权限', $path],
            ['访问权限-更新', 'admin/permission/update', '访问权限', $path],
            ['访问权限-删除', 'admin/permission/destroy', '访问权限', $path],

            ['角色权限-列表', 'admin/role-permission/index', '角色权限', $path],
            ['角色权限-详情', 'admin/role-permission/show', '角色权限', $path],
            ['角色权限-保存', 'admin/role-permission/store', '角色权限', $path],

            ['角色绑定-创建', 'admin/bind-role/create', '角色绑定', $path],
            ['角色绑定-保存', 'admin/bind-role/store', '角色绑定', $path],
        ];

        foreach($root_permissions as $permission){
            [$title, $uri, $pid, $path] = $permission;
            $adminPermission = AdminPermission::where('title', $title)->where('uri', '#')->findOrEmpty();
            //$output->writeln(AdminPermission::getLastSql());
            if ($adminPermission->isExists())
            {
                $output->writeln("{$title}:{$uri} 已存在");
                continue;
            }

            AdminPermission::generateLog($title, $uri, $pid, $path);
            $output->writeln("{$title}:{$uri} 写入成功");
        }

        foreach($permissions as $permission){
            [$title, $uri, $pid, $path] = $permission;
            $adminPermission = AdminPermission::where('title', $title)->where('uri', $uri)->findOrEmpty();
            //dump(AdminPermission::getLastSql());
            if ($adminPermission->isExists())
            {
                $output->writeln("{$title}:{$uri} 已存在");
                continue;
            }

            $adminPermission2 = AdminPermission::where('title', $pid)->findOrEmpty();
            $pid = 0;
            if ($adminPermission2->isExists()) $pid = (int)$adminPermission2->id;

            $path = AdminPermission::generatePath($pid);
            AdminPermission::generateLog($title, $uri, $pid, $path);
            $output->writeln("{$title}:{$uri} 写入成功");
        }

        $output->writeln('end');
    }
}
